//充值
function accountSubmit() {
	var userNum = document.getElementById("userNum").value;
	var amount = document.getElementById("amount").value;
	var userNumError = document.getElementById("userNumError").innerHTML;
	var amountError = document.getElementById("AmountError").innerHTML;
	if(userNum === ""){
		document.getElementById("errorMsg").innerHTML = "请输入用户账号。";
		openWindow("transMsgModal");
		return;
	}
	if(amount === ""){
		document.getElementById("errorMsg").innerHTML = "请输入充值金额。";
		openWindow("transMsgModal");
		return;
	}
	if (userNumError) {
		document.getElementById("errorMsg").innerHTML = "用户账号输入不合法，请确认。";
		openWindow("transMsgModal");
		return;
	}
	if (amountError) {
		document.getElementById("errorMsg").innerHTML = "金额输入不合法，请确认。";
		openWindow("transMsgModal");
		return;
	}
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token,
		"amountAccountNumber" : document.getElementById("userNum").value, // 需要充值的账号
		"amount" : document.getElementById("amount").value, // 充值的金额
		"phone" : document.getElementById("admUserPhone").value, // 充值的用户手机号
		"userName" : document.getElementById("admUserName").value,
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/recharge",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			var data = result.data
			if (data.status) {
				// 查询到的数据
				alert(data.promptMessage);
				queryHistory();
			} else {
				alert(data.promptMessage);
			}
		}
	});
}
// 获取用户信息
function getUserDetail(number) {
	document.getElementById("admUserName").value = "";
	document.getElementById("admUserPhone").value = "";
	if(!number){
		return;
	}
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token,
		"number" : number
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/queryuser",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			var data = result.data;
			if (data) {
				if(data.status === false){
					alert(data.promptMessage);
					return;
				}
				document.getElementById("admUserName").value = data.userName;
				document.getElementById("admUserPhone").value = data.phone;
			} else {
				alert("数据请求错误！");
			}

		}
	});
}
// 校验用户账号
function validUserAccount(account, errorMsgId) {
	document.getElementById(errorMsgId).innerText = "";
	if (!userAccountValidator.test(account)) {
		document.getElementById(errorMsgId).innerText = "请输入正确的账号";
		return false;
	}
}
//校验金额
function validAmount(value,id){
	document.getElementById(id).innerText = "";
	if (!priceValidator.test(value)) {
		document.getElementById(id).innerText = "请输入正确的金额";
		return false;
	}
}
var historyOrder = [];
var currentPage = 1;
var totalPage = 1;
// 查询转入记录
function queryHistory() {
	var options = {
		"accountNumber" : localStorage.accountNum,
		"token" : localStorage.token
	};
	$.ajax({
		type : "POST",
		url : "/ThreeNewBoard/transferquery",
		data : JSON.stringify(options),
		crossDomain : true,
		async : true,
		contentType : "application/json",
		dataType : "json",
		success : function(result) {
			if (result.message == "SUCCESS") {
				// data为请求到的数据
				var data = result.data;
				historyOrder = data;
				totalPage = Math.ceil(data.length / 10);
				if (totalPage === 0) {
					var noData = "<div style='width:100%;height:30px;margin:40px 0;text-align: center;'>暂无表格数据</div>"
					document.getElementById("transferNoData").innerHTML = noData;
				}
				document.getElementById("transferTotal").innerText = "";
				$("#transferTotal").append(totalPage);
				dealData();
			} else {
				alert("数据请求错误！");
			}
		}
	});
			
}

// 处理交易记录表格数据展示
function dealData() {
	var displayOrder = [];
	$("#displayData").find("tr").remove();
	if (totalPage < 1 || currentPage === totalPage) {
		$("#nextPageBtn").attr("disabled", true);
	} else {
		$("#nextPageBtn").attr("disabled", false);
	}
	if (currentPage === 1) {
		$("#prePageBtn").attr("disabled", true);
	} else {
		$("#prePageBtn").attr("disabled", false);
	}
	document.getElementById("currentPage").innerText = currentPage;
	for (var i = (currentPage - 1) * 10; i < currentPage * 10; i++) {
		var item = historyOrder[i];
		if (!item) {
			return;
		}
		displayOrder.push(item);
		row = document.createElement("tr"); // 创建行 
		td1 = document.createElement("td"); // 流水号
		td2 = document.createElement("td"); // 用户账号
		td3 = document.createElement("td"); // 用户名
		td4 = document.createElement("td"); // 用户手机号
		td5 = document.createElement("td"); // 方向
		td6 = document.createElement("td"); // 金额
		td7 = document.createElement("td"); // 时间
		td8 = document.createElement("td"); // 操作账号
		td9 = document.createElement("td"); // 操作人
		td1.appendChild(document.createTextNode(item.serialNumber)); // 为单元格添加内容 
		td2.appendChild(document.createTextNode(item.accountNumber));
		td3.appendChild(document.createTextNode(item.userName));
		td4.appendChild(document.createTextNode(item.phone));
		td5.appendChild(document.createTextNode(item.flow));
		td6.appendChild(document.createTextNode(item.amount));
		td7.appendChild(document.createTextNode(item.operatingTime));
		td8.appendChild(document.createTextNode(item.operationAccount));
		td9.appendChild(document.createTextNode(item.operator));
		row.appendChild(td1);
		row.appendChild(td2);
		row.appendChild(td3);
		row.appendChild(td4);
		row.appendChild(td5);
		row.appendChild(td6);
		row.appendChild(td7);
		row.appendChild(td8);
		row.appendChild(td9);
		$("#displayData").append(row);

	}
}
// 上一页
function prePage() {
	currentPage--;
	dealData();
}
// 下一页
function nextPage() {
	currentPage++;
	dealData();
}